Underlying Platform | Type | Responsibility | # of files in a program | Libraries | Etc | |
---|---|---|---|---|---|---|
Server-side (Node.js) | Operating system | Standalone program; Built-in web server module and other modules |
General purpose programming; Event-driven programming; To access server-side resources |
Usually several | Required or imported from other modules(, i.e., files) | Variables must be declared with let, var, and const. |
Client-side | Web browser | A part of a web program | Event-driven programming; To access/manipulate HTML elements responding to events, such as mouse related and keyboard related events; To communicate with other programs running on other computers |
Usually one | Linked within <script> tag | Variables can be used without declaration with let, var, and const. |
http
, https
, cluster
, fs
, path
, querystring
, and url
are very useful.Type | Explanation | Keywords |
---|---|---|
CommonJS modules | Default in Node.js; Developped before ES modules | exports ..., require(...) |
ECMAScript (ES) modules | JavaScript standard modules | export [default] ..., import ... from |
hello1.js
as a module. (Note that each JS file is a module.) We can export multiple functions.// hello1.js exports.hello = function() { console.log('Hello World!'); } // main1.js const helloooo = require('./hello1'); // Not hello1.js? // When there is no file extension, .js is assumed. // Obviously you can also use a full filename, even with a different file extension. // require() returns an object having the property in 'exports' object from './hello1.js'. helloooo.hello(); //-- at server ---------------------------------------- $ node main1.js
nano
or vi
on PuTTY terminal; or editing with WinSCP and execute commands on Command Terminal with WinSCP;
or editing with WinSCP and executing commands on PuTTY terminal.
Note that there is a button on WinSCP to open PuTTY terminal.
hello2.js
as a module in a different way.
A module is exported as a function.// hello2.js module.exports = function() { console.log('Hello World!'); } // main2.js const helloooo = require('./hello2'); helloooo(); //-- at server ---------------------------------------- $ node main2.js
const helloooo = require('./hello2'); // ./hello2.js or ./hello2/index.js
hello2.sjs
and main2.sjs
.
You can try with the above example.
Default file extension | Examples | ||
---|---|---|---|
CommonJS modules with require/exports | .js | See the examples in Trial 1 and 2. | |
ES modules with import/export | .mjs (or .js with package.json that includes "type":"module") |
// test_export.mjs let age = 20; let name = 'John'; const print = () => { return `${name}'s age is ${age}.`; } export {age, name, print}; // test_import.mjs import {print} from "./test_export.mjs"; console.log(print()); |
$ sudo apt-get update $ sudo apt-get install nodejs $ sudo apt-get install npm
https://nodejs.org/en/download/
npm
is a Node.js package manager.
It is used to install other Node.js modules.$ npm install http-server
http
module?hello.js
- to print 'Hello World' on a console// hello.js console.log('Hello World!'); //-- at server ---------------------------------------- $ node hello.js
hello.js
with your port number, not 8080, and run it.
hello_http.js
- to print 'Hello World' from a web browser using the port number 8080
// hello_http.js
const http = require('http'); // Require 'http' module; We will study it in detail in the next section.
const server = http.createServer((req, res) => { // HTTP server object to deal with HTTP requests from clients
res.writeHead(200); // Sends the head to the client with the reponse number 200
res.end('Hello World!'); // Sends the message back to the client
});
server.listen(8080); // The server listens to the port number 8080.
//-- at server ----------------------------------------
$ node hello_http.js
//-- from a browser on a client -----------------------------------------
http://cs.tru.ca:8080
// Browsers usually try to use https, not http, and the above URL may not be reachable.
// You can try to use 198.162.21.132 instead of cs.tru.ca.
hello_http.js
with your port number, not 8080, and run it.
index.html
assumed? What is happening here?